package com.ruoyi.web.controller.edh;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.edh.domain.EdhReturnGoods;
import com.ruoyi.edh.domain.dto.EdhReturnGoodsDto;
import com.ruoyi.edh.domain.model.EdhReturnGoodsModel;
import com.ruoyi.edh.domain.vo.EdhReturnGoodsVo;
import com.ruoyi.edh.service.IEdhReturnGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 易订购-退回退货Controller
 *
 * @author xxx
 * @date 2020-09-03
 */
@RestController
@RequestMapping("/edh/returngoods")
@Api(description = "易订购-退回退货")
public class EdhReturnGoodsController extends BaseController {
    @Autowired
    private IEdhReturnGoodsService edhReturnGoodsService;

    /**
     * 查询易订购-退回退货列表
     */
    @GetMapping("/list")
    @ApiOperation("易订购-退回退货列表")
    public TableDataInfo list(EdhReturnGoods edhReturnGoods) {
        startPage();
        List<EdhReturnGoods> list = edhReturnGoodsService.selectEdhReturnGoodsList(edhReturnGoods);
        return getDataTable(list);
    }

    /**
     * 查询易订购-退回退货列表
     */
    @GetMapping("/volist")
    @ApiOperation("易订购-退回退货Vo列表")
    public TableDataInfo list(EdhReturnGoodsModel.SearchModel edhReturnGoods) {
        startPage();
        List<EdhReturnGoodsVo> list = edhReturnGoodsService.selectEdhReturnGoodsVoList(edhReturnGoods);
        return getDataTable(list);
    }

    /**
     * 导出易订购-退回退货列表
     */
    @Log(title = "易订购-退回退货", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出易订购-退回退货列表")
    public AjaxResult export(EdhReturnGoods edhReturnGoods) {
        List<EdhReturnGoods> list = edhReturnGoodsService.selectEdhReturnGoodsList(edhReturnGoods);
        ExcelUtil<EdhReturnGoods> util = new ExcelUtil<EdhReturnGoods>(EdhReturnGoods.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 导出易订购-退回退货列表
     */
    @Log(title = "易订购-退回退货VO", businessType = BusinessType.EXPORT)
    @GetMapping("/voexport")
    @ApiOperation("导出易订购-退回退货VO列表")
    public AjaxResult export(EdhReturnGoodsModel.SearchModel edhReturnGoods) {
        List<EdhReturnGoodsVo> list = edhReturnGoodsService.selectEdhReturnGoodsVoList(edhReturnGoods);
        ExcelUtil<EdhReturnGoodsVo> util = new ExcelUtil<EdhReturnGoodsVo>(EdhReturnGoodsVo.class);
        return util.exportExcel(list, "退货列表");
    }

    /**
     * 获取易订购-退回退货详细信息
     */
    @GetMapping("/{id}")
    @ApiOperation("获取易订购-退回退货详细信息")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(edhReturnGoodsService.selectEdhReturnGoodsById(id));
    }

    /**
     * 新增易订购-退回退货
     */
    @Log(title = "易订购-退回退货", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增易订购-退回退货")
    public AjaxResult add(@RequestBody EdhReturnGoodsDto edhReturnGoodsDto) {

        Boolean aBoolean = edhReturnGoodsService.insertEdhReturnGoods(edhReturnGoodsDto);
        if (aBoolean) {
            return AjaxResult.success("申请退货退款成功");
        }
        return AjaxResult.error("申请退货退款时发生失败");
    }

    /**
     * 新增易订购-申请售后接口
     */
    @Log(title = "申请售后接口", businessType = BusinessType.INSERT)
    @PostMapping("/addReturn")
    @ApiOperation("申请售后接口")
    public AjaxResult addReturn(EdhReturnGoods edhReturnGoods) {
        return toAjax(edhReturnGoodsService.addReturn(edhReturnGoods));
    }

    ;

    /**
     * 修改易订购-退回退货
     */
    @Log(title = "易订购-退回退货", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改易订购-退回退货")
    public AjaxResult edit(@RequestBody EdhReturnGoods edhReturnGoods) {
        return toAjax(edhReturnGoodsService.updateEdhReturnGoods(edhReturnGoods));
    }

    /**
     * 删除易订购-退回退货
     */
    @Log(title = "易订购-退回退货", businessType = BusinessType.DELETE)
    @DeleteMapping
    @ApiOperation("删除易订购-退回退货")
    public AjaxResult remove(Long[] ids) {
        return toAjax(edhReturnGoodsService.deleteEdhReturnGoodsByIds(ids));
    }

    /**
     * 审核服务单
     *
     * @param id           退货单id
     * @param isAudit      1 审核通过 2 审核不通过
     * @param returnStatus 0 未收到货物 1已收到货物
     * @param reason       理由
     */
    @GetMapping("/auditReturnGoods")
    @ApiOperation("审核退货单")
    public AjaxResult auditReturnGoods(Long id, String isAudit, String returnStatus, String reason) {
        if (StringUtils.isEmpty(reason)) {
            return AjaxResult.error("请输入审核备注");
        }
        edhReturnGoodsService.auditReturnGoods(id, isAudit, returnStatus, reason);
        return AjaxResult.success("审核成功");
    }

    /**
     * 查询退货单各种状态的数据
     */
    @GetMapping("/getStatusNumber")
    @ApiOperation("查询退货单各种状态的数据")
    public AjaxResult getStatusNumber() {
        return AjaxResult.success(edhReturnGoodsService.getStatusNumber());
    }

    /**
     * 根据订单id获取退货详情
     */
    @GetMapping("/getDetail")
    @ApiOperation("根据订单id获取退货详情")
    public AjaxResult getDetail(Long orderId) {
        return AjaxResult.success(edhReturnGoodsService.getDetail(orderId));
    }
}
